//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	///		Summary description for ReferralsListControl.
	/// </summary>
	public class ReferralsListControl : UserControl
	{
		protected DropDownList filterPost;
		protected Repeater referrals;
		protected IPagedControl pager;

		protected BaseBlogPage BlogPage
		{
			get {return this.Page as BaseBlogPage;}
		}

		protected Weblog CurrentWeblog
		{
			get { return this.BlogPage.CurrentWeblog;}
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack && !BlogPage.IsCallBack)
			{
				ConfigurePage();
				buildGrid();
			}
		}

		protected void buildGrid()
		{
			CSContext context = CSContext.Current;

			int pid = context.PostID;

			BlogThreadQuery query = new BlogThreadQuery();
			query.SectionID = this.CurrentWeblog.SectionID;
			query.BlogPostType = BlogPostType.Post | BlogPostType.Article;
			query.PageIndex = context.PageIndex;

			if (pid == -1)
			{
				query.PageSize = Globals.SafeInt(context.User.GetExtendedAttribute("CPPageSize"),10);
				pager.Visible = true;
			}
			else
			{
				query.PageSize = int.MaxValue;
				pager.Visible = false;
			}

			Referral r = new Referral();
			r.SectionID = this.CurrentWeblog.SectionID;
			r.PostID = pid;

			ReferralSet rs = CommunityServer.Components.Referrals.GetReferrals(r, query.PageSize, query.PageIndex);

			foreach (Referral r2 in rs.Referrals)
			{
				r2.ViewPostURL = BlogUrls.Instance().ShortLink(r2.PostID);
				r2.Url = Context.Server.HtmlEncode(r2.Url);
			}

			referrals.DataSource = rs.Referrals;
			referrals.DataBind();

			pager.PageIndex = query.PageIndex;
			pager.PageSize = query.PageSize;
			pager.TotalRecords = rs.TotalRecords;

		}

		void ConfigurePage()
		{
			// Retrieve list of blog posts
			BlogThreadQuery query = new BlogThreadQuery();
			query.SectionID = this.CurrentWeblog.SectionID;
			query.BlogPostType = BlogPostType.Post | BlogPostType.Article;
			query.BlogThreadType = BlogThreadType.Recent;
			query.IncludeCategories = false;
			query.PublishedFilter = BlogPostPublishedFilter.All;
			query.PageSize = int.MaxValue;
			query.PageSize = 20; //Do we want to return all values to the dropdown
			query.SortOrder = SortOrder.Descending;
			query.SortBy = BlogThreadSortBy.MostRecent;
			ThreadSet threads = WeblogPosts.GetBlogThreads(query,false);

			// Insert default "All" item
			filterPost.Items.Add(new ListItem(CommunityServer.ControlPanel.Components.ResourceManager.GetString("Feedback_FilterAll"), "-1"));

			foreach (WeblogPost post in threads.Threads)
			{
				filterPost.Items.Add(new ListItem(Globals.HtmlDecode(post.Subject), Convert.ToString(post.PostID)));
			}

			// Check if preselected values of the filter DropDownLists were passed in the URL
			CSContext context = CSContext.Current;
			ListItem li = null;

			int pid = context.PostID;

			if(pid > -1)
			{
				li = filterPost.Items.FindByValue(pid.ToString());
				if(li != null)
				{
					li.Selected = true;
				}
			}
		}

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
			this.filterPost.SelectedIndexChanged += new EventHandler(this.filterPost_SelectedIndexChanged);
		}
		

		private void filterPost_SelectedIndexChanged(object sender, EventArgs e)
		{
			string url  = "{0}?tab=comments&postid={1}";
			url = string.Format(url, Request.Path, filterPost.SelectedValue);
			Response.Redirect(url);
		}
	}
}
